package com.itheima.aspect;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.util.Date;

@Component
@org.aspectj.lang.annotation.Aspect
public class Aspect1 {

    @Pointcut("execution(* com.itheima.service.EquipmentService.add*(..))")
    private void pt(){}

    @Around("pt()")
//获取参数名和参数值
    public Object getParam(ProceedingJoinPoint pjp) throws Throwable {

        try {
            Object obj=pjp.getArgs()[0];

            Class<?> aClass = obj.getClass();
            String jsonStr= JSONObject.toJSONString(aClass);
            System.out.println(aClass);
            Field created = aClass.getDeclaredField("created");
            Field updated = aClass.getDeclaredField("updated");
            created.setAccessible(true);
            updated.setAccessible(true);
            created.set(obj,new Date());
            updated.set(obj,new Date());


        } catch (Throwable e) {
            System.out.println(e);
        } finally {
        }
        return pjp.proceed(pjp.getArgs());
    }
}



